""" 
触摸开关传感器
功能说明: 
    https://blog.csdn.net/m0_61036291/article/details/140498200

工作原理:
    电容式触摸开关传感器
    当手指触摸到传感器时，传感器内部的电容充电，充电后电压达到一定值，输出引脚会输出高电平，
    未触摸时输出低电平。

接线:
    DO --> gpio2
        未触摸: 低电平
        触摸: 高电平
    VCC --> 5V
    GND --> GND

实验现象:

"""

from machine import Pin, ADC
import time

DO_PIN_NUM = 2  # 触摸开关传感器DO
LED_PIN_NUM = 15  # LED端口
Do_Trigger_Type = 1
count = 0

do_pin = None
led_pin = None


def init_setup():
    global DO_PIN_NUM, LED_PIN_NUM
    global do_pin, led_pin
    global Do_Trigger_Type

    # 1.初始化D0
    """1.input 低电平触发"""
    Pin_Pull_Type = Pin.PULL_DOWN
    if Do_Trigger_Type == 0:
        """2.input 高电平触发"""
        Pin_Pull_Type = Pin.PULL_UP
    do_pin = Pin(DO_PIN_NUM, Pin.IN, Pin_Pull_Type)
    # 2.初始化led
    led_pin = Pin(LED_PIN_NUM, Pin.OUT)  # 输出
    led_pin.value(0)


def loop_run():
    global do_pin
    global Do_Trigger_Type

    while True:
        value = do_pin.value()
        if value == Do_Trigger_Type:
            led_pin.value(1)
            print("触摸开 +++")
        else:
            led_pin.value(0)
            print("未触摸 ---")

        time.sleep(1)


# 程序入口
if __name__ == "__main__":
    init_setup()
    loop_run()
